package 
{
   import flash.display.Sprite;
   import com.qiyi.player.wonder.body.view.AppView;
   import flash.events.Event;
   import flash.system.Security;
   import com.qiyi.player.wonder.common.config.FlashVarConfig;
   import com.qiyi.player.wonder.common.sw.SwitchManager;
   import flash.media.SoundTransform;
   import flash.media.SoundMixer;
   import com.qiyi.player.core.model.utils.LogManager;
   import com.qiyi.player.core.CoreManager;
   import com.qiyi.player.core.model.def.PlatformEnum;
   import com.qiyi.player.core.model.def.PlayerTypeEnum;
   import com.qiyi.player.base.uuid.UUIDManager;
   import flash.display.StageAlign;
   import flash.display.StageScaleMode;
   import com.iqiyi.components.tooltip.ToolTip;
   import com.iqiyi.components.global.GlobalStage;
   import com.qiyi.player.wonder.common.lso.LSO;
   import com.qiyi.player.base.pub.ProcessesTimeRecord;
   import flash.utils.getTimer;
   import com.qiyi.player.wonder.AppFacade;
   import flash.system.Capabilities;
   import flash.external.ExternalInterface;
   import flash.net.URLRequest;
   import flash.net.sendToURL;
   
   public class Player extends Sprite
   {
      
      public function Player() {
         super();
         if(stage)
         {
            this.onAddToStage();
         }
         else
         {
            addEventListener(Event.ADDED_TO_STAGE,this.onAddToStage);
         }
      }
      
      private var _appView:AppView;
      
      public function get appView() : AppView {
         return this._appView;
      }
      
      private function onAddToStage(param1:Event = null) : void {
         removeEventListener(Event.ADDED_TO_STAGE,this.onAddToStage);
         Security.allowDomain("*");
         Security.allowInsecureDomain("*");
         this.initProcessesTimeInfo();
         this.sendTest1PB();
         this.init();
      }
      
      private function init() : void {
         this.initFlashVar();
         this.initCore();
      }
      
      private function initFlashVar() : void {
         FlashVarConfig.init(loaderInfo.parameters);
         SwitchManager.getInstance().initByFlashVar(FlashVarConfig.components);
      }
      
      private function initCore() : void {
         var _loc3_:SoundTransform = null;
         if((loaderInfo.parameters.hasOwnProperty("masflag")) && loaderInfo.parameters.masflag == "true")
         {
            _loc3_ = new SoundTransform();
            _loc3_.volume = 0;
            SoundMixer.soundTransform = _loc3_;
         }
         LogManager.initLog(FlashVarConfig.owner == FlashVarConfig.OWNER_PAGE);
         var _loc1_:* = "";
         if(loaderInfo.parameters.flashP2PCoreUrl)
         {
            _loc1_ = loaderInfo.parameters.flashP2PCoreUrl.toString();
         }
         var _loc2_:Boolean = CoreManager.getInstance().initialize(stage,PlatformEnum.PC,PlayerTypeEnum.MAIN_STATION,_loc1_);
         if(_loc2_)
         {
            this.onCoreInitComplete();
         }
         else
         {
            CoreManager.getInstance().addEventListener(CoreManager.Evt_InitComplete,this.onCoreInitComplete);
         }
         UUIDManager.instance.setWebEventID(FlashVarConfig.webEventID);
      }
      
      private function initStage() : void {
         stage.align = StageAlign.TOP_LEFT;
         stage.scaleMode = StageScaleMode.NO_SCALE;
         ToolTip.getInstance().init(stage);
         GlobalStage.initStage(stage);
      }
      
      private function initLSO() : void {
         LSO.getInstance().init();
      }
      
      private function initAppView() : void {
         this._appView = new AppView();
         addChild(this._appView);
      }
      
      private function initProcessesTimeInfo() : void {
         var curDate:Date = null;
         var diff:Number = NaN;
         try
         {
            if((loaderInfo.parameters.hasOwnProperty("browserType")) && !(loaderInfo.parameters.browserType == ""))
            {
               ProcessesTimeRecord.browserType = String(loaderInfo.parameters.browserType);
            }
            if((loaderInfo.parameters.hasOwnProperty("pageTmpltType")) && !(loaderInfo.parameters.pageTmpltType == ""))
            {
               ProcessesTimeRecord.pageTmpltType = String(loaderInfo.parameters.pageTmpltType);
            }
            if((loaderInfo.parameters.hasOwnProperty("playerCTime")) && Number(loaderInfo.parameters.playerCTime) > 0)
            {
               curDate = new Date();
               diff = curDate.getTime() - Number(loaderInfo.parameters.playerCTime);
               if(diff > 0)
               {
                  ProcessesTimeRecord.usedTime_selfLoaded = diff;
               }
            }
         }
         catch(e:Error)
         {
            ProcessesTimeRecord.browserType = "";
            ProcessesTimeRecord.pageTmpltType = "";
            ProcessesTimeRecord.usedTime_selfLoaded = 0;
         }
      }
      
      private function onCoreInitComplete(param1:Event = null) : void {
         this.initStage();
         this.initLSO();
         this.initAppView();
         if(FlashVarConfig.autoPlay)
         {
            ProcessesTimeRecord.STime_showVideo = getTimer();
         }
         else
         {
            ProcessesTimeRecord.needRecord = false;
         }
         AppFacade.getInstance().startup(this);
      }
      
      private function sendTest1PB() : void {
         var _loc1_:* = "";
         if((loaderInfo) && (loaderInfo.parameters))
         {
            _loc1_ = loaderInfo.parameters.yhls;
         }
         var _loc2_:String = "http://msg.video.qiyi.com/tmpstats.gif?type=yhls20130924&usract=jingyitest1" + "&url=" + this.getPageLocationHref() + "&ver=" + encodeURIComponent(Capabilities.version) + "&yhls=" + _loc1_ + "&tn=" + Math.random();
         this.sendData(_loc2_);
      }
      
      private function getPageLocationHref() : String {
         var location:String = "";
         try
         {
            location = ExternalInterface.call("function(){return window.location.href;}");
            if(location)
            {
               location = encodeURIComponent(location);
            }
         }
         catch(e:Error)
         {
            location = "";
         }
         return location;
      }
      
      private function sendData(param1:String) : void {
         var _loc2_:URLRequest = new URLRequest();
         _loc2_.url = param1;
         sendToURL(_loc2_);
      }
   }
}
